﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace NameOfSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            var p = new Program();

            //var name = p.NameOf(t => t.MethodToTest(null));
            //Console.WriteLine("Name: {0}", name);

            var name  = p.NameOf<Program>(q => q.MethodToTest);
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        public Program()
        {
            //var name = this.NameOf<Program>(r => r.Property);
            //var name2 = Name2();
        }

        public ActionResult MethodToTest(ActionContext context)
        {
            return null;
        }

        public int Property { get; set; }

        public string NameOf<T>(Expression<Action<T>> expression)
        {
            return null;
        }

        private string NameOf<T>(Expression<Func<T, Func<ActionContext, ActionResult>>> expression)
        {
            if (expression == null) { throw new ArgumentNullException("expression"); }

            var body = (UnaryExpression)expression.Body;
            var operand = (MethodCallExpression)body.Operand;
            var obj = (ConstantExpression)operand.Object;
            var value = (MethodInfo)obj.Value;
            
            return value.Name;
        }

        public void Method2(object dt)
        {
        }
    }

    internal class ActionResult
    {
    }

    internal class ActionContext
    {
    }
}
